# encoding: utf-8
"""
@author: 夏洛
@QQ: 1972386194
@file: 03-request-cache.py
"""



import requests_cache
import requests,time

def tses_time():
    star_time = time.time()
    session = requests.session()
    for i in range(1,10):
        session.get('http://httpbin.org/delay/1')
        print(f'xxx {i+1} requests' )
    end_time = time.time() -  star_time
    print(end_time)
    # 13.285794973373413

def test_req_cache():
    # 需要数据库来存放 指纹信息
    star_time = time.time()
    requests_cache.install_cache('demo_cache', backend='filesystem', use_cache_dir=True)
    xx = requests.session()
    for i in range(1,10):
        # 对指纹进行比对  有请求过就不发第2次请求
        xx.get('http://httpbin.org/delay/1')
        print(f'xxx {i+1} requests' )
    end_time = time.time() - star_time
    print(end_time)
    # 1.5862023830413818



requests_cache.install_cache('demo_cache2', allowable_methods=['GET'])
start = time.time()
session = requests.Session()
for i in range(10):
    session.get('http://httpbin.org/delay/1')
    print(f'Finished {i + 1} requests')
end = time.time()
print('Cost time for get', end - start)
start = time.time()

for i in range(10):
    session.post('http://httpbin.org/delay/1')
    print(f'Finished {i + 1} requests')
end = time.time()
print('Cost time for post', end - start)













